"""
活动记录相关数据模型
"""
from datetime import datetime
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field
from bson import ObjectId
from .base import PyObjectId

class ActivityLog(BaseModel):
    """活动记录模型"""
    user_id: str = Field(..., description="用户ID")
    action: str = Field(..., description="操作类型")
    target_type: str = Field(..., description="目标类型")
    target_id: str = Field(..., description="目标ID")
    target_name: str = Field(..., description="目标名称")
    description: str = Field(..., description="操作描述")
    ip_address: Optional[str] = Field(None, description="IP地址")
    user_agent: Optional[str] = Field(None, description="用户代理")
    metadata: Dict[str, Any] = Field(default_factory=dict, description="元数据")

class ActivityLogInDB(ActivityLog):
    """数据库中的活动记录模型"""
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="创建时间")
    
    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}

class ActivityLogResponse(ActivityLog):
    """活动记录响应模型"""
    id: str = Field(..., description="记录ID")
    user_name: Optional[str] = Field(None, description="用户名")
    created_at: datetime = Field(..., description="创建时间")

class ActivityLogList(BaseModel):
    """活动记录列表响应模型"""
    total: int = Field(..., description="总记录数")
    page: int = Field(..., description="当前页码")
    limit: int = Field(..., description="每页数量")
    data: List[ActivityLogResponse] = Field(..., description="活动记录列表")

class LoginLog(BaseModel):
    """登录日志模型"""
    user_id: str = Field(..., description="用户ID")
    ip_address: str = Field(..., description="IP地址")
    user_agent: str = Field(..., description="用户代理")
    device_info: Optional[str] = Field(None, description="设备信息")
    success: bool = Field(..., description="登录是否成功")

class LoginLogInDB(LoginLog):
    """数据库中的登录日志模型"""
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    login_time: datetime = Field(default_factory=datetime.utcnow, description="登录时间")
    
    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}

class LoginLogResponse(LoginLog):
    """登录日志响应模型"""
    id: str = Field(..., description="日志ID")
    login_time: datetime = Field(..., description="登录时间")

class LoginLogList(BaseModel):
    """登录日志列表响应模型"""
    total: int = Field(..., description="总记录数")
    page: int = Field(..., description="当前页码")
    limit: int = Field(..., description="每页数量")
    data: List[LoginLogResponse] = Field(..., description="登录日志列表")